package com.just_bean.core.aop;

import com.just_bean.core.handler.JustMethodProxy;

import java.lang.reflect.Proxy;
import java.util.List;

public class InterfaceClz extends AopClz {

    public InterfaceClz(Class clz) {
        super(clz);
    }

    @Override
    public Object newInstance() {
        Object target = new Object();
        return Proxy.newProxyInstance(clz.getClassLoader(), new Class[]{clz}, (proxy, method, args) -> {
            List<JustMethodProxy> list = aopMap.get(method);
            if (list == null) return method.invoke(target, args);
            for (JustMethodProxy preMethod : list) {
                preMethod.pre(method, clz, target, args);
            }
            Object result = null;
            for (JustMethodProxy afterMethod : list) {
                result = afterMethod.after(method, clz, target, args, result);
            }
            return result;
        });
    }
}
